package top.wangxiaomei.ai.service.impl;

import com.google.gson.Gson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import top.wangxiaomei.ai.domain.Models;
import top.wangxiaomei.ai.other.Abs;
import top.wangxiaomei.ai.other.Meaning;
import top.wangxiaomei.ai.other.WordEX;
import top.wangxiaomei.ai.service.SenService;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
@Service
public class SenServiceImpl implements SenService {
    @Autowired
    public WordsServiceImpl wordsService;
    @Autowired
    public ModelsServiceImpl modelsService;

    // 缓存
    HashMap<String, ArrayList<WordEX>> cache = new HashMap<>();
    @Override
    public ArrayList<WordEX> getWords(String sen) {
        if (cache.containsKey(sen)) {
            return cache.get(sen);
        }
        ArrayList<WordEX> words = new ArrayList<>();
        for (int i = 0; i < sen.length(); i++) {
            for (int len = 1; len <= sen.length() - i; len++) {
                String word = sen.substring(i, i+len);
                List<Abs> ex = wordsService.getEx(word);
                ex.addFirst(new Abs(word, 1.0f));
                if (ex.size()==1) continue;

                words.add(new WordEX(i,word, ex));
            }
        }
        cache.put(sen, words);
        return words;
    }
    public void getSens(ArrayList<WordEX> state, ArrayList<WordEX> choices, int stateLen, int target, int lastIndex,ArrayList<ArrayList<WordEX>> sen) {
        if (stateLen == target) {
            ArrayList<WordEX> meanings = new ArrayList<>(state);
            sen.add(meanings);
            return;
        }
        for (int i = lastIndex; i < choices.size(); i++) {
            WordEX wordEX = choices.get(i);
            if (stateLen != wordEX.getIndex()) continue; // 如果当前单词的起始位置与状态长度不匹配，则跳过
            state.add(wordEX);
            // 进行下一轮选择
            getSens(state, choices, stateLen + wordEX.getLen(), target,i+1,sen);
            // 回退：撤销选择，恢复到之前的状态
            state.removeLast();
        }
    }

    public ArrayList<ArrayList<WordEX>> getSens(ArrayList<WordEX> words, int senLen) {
        ArrayList<ArrayList<WordEX>> sen = new ArrayList<>();
        getSens(new ArrayList<>(),words,0,senLen,0,sen);
        return sen;
    }

    @Override
    public ArrayList<Meaning> getMeaning(ArrayList<WordEX> meaning,ArrayList<Meaning> Sen) {
        ArrayList<Meaning> reslut = new ArrayList<>();
        Meaning temp = new Meaning(0,meaning);
        ArrayList<Meaning> meanList = new ArrayList<>();
        meanList.add(temp);
        while (!meanList.isEmpty()){
            Meaning meaning1 = meanList.removeFirst();
            if (meaning1.getMeaning().size()==1){
                reslut.add(meaning1);
                continue;
            }
            int index = Math.max(meaning1.getI() - 1, 0);

            for (int i = index; i < meaning1.getMeaning().size(); i++) {
                ArrayList<WordEX> wordEXES = new ArrayList<>(meaning1.getMeaning());
                if (i+1 >= wordEXES.size()) continue;
                WordEX left = wordEXES.get(i);

                WordEX right = wordEXES.get(i+1);


                List<Abs> ex = modelsService.machModel(left,right,null);

                if (ex.isEmpty()){
                    continue;
                }

                Sen.add(new Meaning(i,wordEXES));

                WordEX wordEX = new WordEX(0, left.getWord()+right.getWord(), ex);
                Gson gson = new Gson();
                System.out.println(gson.toJson(wordEX.getEx()));

                wordEXES.set(i,wordEX);

                wordEXES.remove(i+1);

                Meaning temp1 = new Meaning(i,wordEXES);

                meanList.add(temp1);
            }
        }


        return reslut;
    }

    private ArrayList<String> toList(ArrayList<Models> models) {
        ArrayList<String> result = new ArrayList<>();
        for (Models model : models) {
            result.add(model.getModel());
        }
        return result;
    }
}
